package com.kadron.news;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KNewsDAO;
import com.kadron.entity.News;
import com.kadron.manager.KMessageManager;

@AutoCreate
@Name("kNewsManager")
@Scope(ScopeType.PAGE)
public class KNewsManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4184026689307942094L;

	private static final Log log = LogFactory.getLog(KNewsManager.class);
	
	private News news; 
	
	@In
	private KNewsDAO newsDAO; 
	
	@Create
	public void init() {
		Long newsId = (Long) Component.getInstance("newsId");
		if (newsId == null) {
			newsId = new Long(0);
		}
		if (newsId > 0) {
			news = (News) newsDAO.find(newsId, News.class);
		} else {
			news = new News();
		}
	}
	
	public String save() {
		if (news == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		if (log.isDebugEnabled()) {
			log.debug("Save News newsId="+news.getId());
		}
		newsDAO.update(news);
		good();
		return "news";
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}
}
